import { format as formatDate } from '@/filters/date';

export const getValueResultTag = (key, value, filterList, loadMoreSelectCache) => {
  const current = filterList.find(i => i.keyword.toString() === key.toString());
  if (!current) return null;
  const { name, type, multiple, format } = current;
  if (type === 'select') {
    const tempValKey = current.valueName || 'value';
    const tempLabelKey = current.labelName || 'label';
    const opt = current.options.find(i => i[tempValKey] === value);
    return opt ? { name, key, value: opt[tempLabelKey] } : { name, key, value };
  } else if (type === 'loadMoreSelect') {
    if (multiple) {
      return { name, key, value: value.join(',') };
    }
    if (current._showValue) return { name, key, value: current._showValue || value };
    const cache = loadMoreSelectCache[current.keyword];
    return { name, key, value: cache && cache._showValue || value };
  } else if (type === 'mulSelect' || (type !== 'cascaderSelect' && multiple)) {
    // 多选有远程下拉多选的类型
    const target = [];
    const optionsList = current.options;
    const label = current.labelName || 'label';
    const keyName = current.valueName || 'value';
    if (!value) return;
    value.forEach(val => {
      const opt = optionsList.find(i => i[keyName] === val);
      opt && target.push(opt[label]);
    });
    if (target.length > 0) {
      return { name, key, value: target.join(',') };
    } else {
      return null;
    }
  } else if (type === 'cascaderSelect' || type === 'cascaderSelectWithChild') {
    const target = [];
    if (!value) return;
    if (Array.isArray(value)) {
      value.forEach(val => {
        const opt = findOptions(current.options, val, current.valueName, current.childrenVal);
        if (opt) target.push(opt[current.labelName || 'name']);
      });
      if (target.length > 0) {
        return { name, key, value: target.join(current.separator || ',') };
      } else {
        return null;
      }
    } else {
      const opt = findOptions(current.options, value, current.valueName, current.childrenVal);
      return opt ? { name, key, value: opt[current.labelName || 'name'] } : { key, value };
    }
  } else if (type === 'inputRange' || type === 'inputRadioRange') {
    const hasVal = value && Array.isArray(value) && value.length === 2 && ((value[0] || value[0] === 0) || (value[1] || value[1] === 0));
    let valueLabel = '';
    if (hasVal) {
      if ((value[0] || value[0] === 0) && (value[1] || value[1] === 0)) {
        const cloneVal = JSON.parse(JSON.stringify(value));
        if (current.labelUnit) {
          if (cloneVal[0] !== 0) cloneVal[0] += current.labelUnit;
          if (cloneVal[1] !== 0) cloneVal[1] += current.labelUnit;
        }
        valueLabel = cloneVal.join('-');
      } else if (!value[0] && value[0] !== 0) {
        valueLabel = `<= ${value[1]}${current.labelUnit || ''}`;
      } else {
        valueLabel = `>= ${value[0]}${current.labelUnit || ''}`;
      }
    }
    if (valueLabel) return { name, key, value: valueLabel };
    return null;
  } else if (type === 'date') {
    const newFormat = format ? format.toUpperCase() : 'YYYY/MM/DD';
    return { name, key, value: formatDate(value, newFormat) };
  } else if (type === 'rangeDate') {
    const newFormat = format ? format.toUpperCase() : 'YYYY/MM/DD';
    const start = value[0];
    const end = value[1];
    return { name, key, value: `${formatDate(start, newFormat)}-${formatDate(end, newFormat)}` };
  }
  return { name, key, value };
};
export const removeEmptyFilter = (data) => {
  const filterData = _.cloneDeep(data);
  const model = ['filterModel', 'filterOptions'];
  model.forEach(i => {
    if (filterData[i]) {
      Object.keys(filterData[i]).forEach(key => {
        const normalEmpty = !filterData[i][key] && filterData[i][key] !== 0;
        const arrayEmpty = Array.isArray(filterData[i][key]) && filterData[i][key].length === 0;
        // 成本信息没有选择类型或输入内容，则不进行筛选
        const objEmpty = filterData[i][key] && typeof filterData[i][key] === 'object' &&
        filterData[i][key].isCost &&
        (!isNotNill(filterData[i][key].costType) || (!filterData[i][key].value && filterData[i][key].value !== 0));

        const rangeEmpty = filterData[i][key] && typeof filterData[i][key] === 'object' && filterData[i][key].isRange &&
        !isNotNill(filterData[i][key].start) && !isNotNill(filterData[i][key].end);

        if (normalEmpty || arrayEmpty || objEmpty || rangeEmpty) {
          delete filterData[i][key];
        }
      });
    }
  });
  return filterData;
};

function findOptions(options, value, valueName = 'id', childrenVal = 'item') {
  const callback = (opt, val) => {
    if (opt && opt.length === 0) return null;
    for (let i = 0; i < opt.length; i++) {
      if (opt[i][valueName] === val) {
        return opt[i];
      } else if (opt[i][childrenVal]) {
        const res = callback(opt[i][childrenVal], val);
        if (res) {
          return res;
        }
      }
    }
  };
  const a = callback(options, value);
  return a;
}

function isNotNill(num) {
  return num || num === 0;
}
